package JZOffer;

import java.util.Arrays;

// 调整数组顺序使奇数位于偶数前面
public class JzOffer21 {
    public static int[] exchange(int[] nums) {
        int l = 0;
        int r = nums.length-1;

        while (l < r) {
            if (nums[l] % 2 == 0 && nums[r] % 2 != 0) {
                int tmp = nums[l];
                nums[l] = nums[r];
                nums[r] = tmp;
            }
            if (nums[l] % 2 != 0) {
                l++;
            }
            if (nums[r] % 2 == 0) {
                r--;
            }
        }
        return nums;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(exchange(new int[]{1, 2, 3, 4})));
    }
}
